/**
 * Created by Xiaoming on 2017/9/4.
 */
angular.module('module.menu')
    .controller('StoreController', StoreController);
StoreController.$inject = ['$state', '$cookies', '$stateParams', 'ShopMenuService', 'ShopService', 'CouponService', 'AuthService'];
function StoreController($state, $cookies, $stateParams, ShopMenuService, ShopService, CouponService, AuthService) {
    var vm = this;
    vm.phone_hide = phone_hide;
    vm.shop_coupon = shop_coupon;
    vm.goshopinfo = goshopinfo;
    vm.goorderhistory = goorderhistory;
    vm.gologin = gologin;
    vm.chooseSku = chooseSku;
    vm.closeSku = closeSku;
    vm.tab = tab;
    vm.gosettle = gosettle;
    vm.float_more = float_more;
    vm.sure_clean = sure_clean;
    vm.notrash = notrash;
    vm.remove = remove;
    vm.add = add;
    vm.go_back = go_back;
    vm.store_hide = store_hide;
    vm.addthis = addthis;
    vm.enlargeClose = enlargeClose;
    vm.enlarge = enlarge;
    vm.renderFinish = renderFinish;
    vm.gotocoupon = gotocoupon;
    //强制跳转用户微信登陆页面
    //if (!$cookies.get("_userId")) {
    //    var redirect_uri = window.location.href.replace("/store/" + $stateParams.shopid + "?", "/wxuc?shopid=" + $stateParams.shopid + "&");
    //    //var redirect_uri = window.location.href;
    //    function success_cbfunc(userinfo) {
    //        AuthUserService.addUserToCookies(userinfo);
    //        setTimeout(function () {
    //            window.history.back();
    //        }, 500);
    //    }
    //    function error_cbfunc(error, openid) {
    //        alert(JSON.stringify(error));
    //    }
    //    WxAuthService.wxTryLoginOrRegist(redirect_uri, UserinfoService.getUserinfoByOpenid, success_cbfunc, error_cbfunc);
    //}
    vm.shopid = $stateParams.shopid;
    CouponService.getByshopid(vm.shopid).$promise.then(function (data) {
        vm.coupon = data;
        for (var i = data.length - 1; i >= 0; i--) {
            if (vm.coupon[i].num == 0) {
                vm.coupon.splice(i, 1);
            }
        }
        if (data.length > 1) {
            vm.coupon = {
                num: data.length,
                jush: "张",
            }
        }
        else if (data.length == 1) {
            vm.coupon.jush = "元";
            vm.coupon.num = data[0].discount;
        }
    });
    function gotocoupon() {
        $state.go("receivecoupon");
    }
    //存储shopid,deskid,appid
    $cookies.put("shopid", $stateParams.shopid);
    if ($stateParams.deskid) {
        vm.deskid = $stateParams.deskid;
        $cookies.put("deskid", vm.deskid);
    } else {
        vm.deskid = '';
        $cookies.put("deskid", vm.deskid);
    }
    vm.shopid = $stateParams.shopid;
    vm.appid = $stateParams.appid;
    vm.scope = $stateParams.scope;
    loadCart(true, "loader");

    //获取起送价
    vm.shopPromotionList = [];
    ShopService.getShopByShopId(vm.shopid).$promise.then(function (shop) {
        vm.shops = shop;
        if (vm.shops.promotionList.length == 0) {
            var promotion = {
                "description": "本店暂无优惠活动"
            };
            vm.shops.promotionList.push(promotion);
        }
        vm.shopGoodList = ShopMenuService.getMenuByShopid($stateParams.shopid);
        if (vm.shops.state == 1) {
            vm.myval = true;
        }
    });
    function renderFinish() {
        new transformScrolls('#storeLeft', '#storeRight');
        new activeTextAnimation();    //店铺页面活动轮播效果
    }
    //图片点击放大
    function enlarge(cid, goods) {
        vm.thisgoods = goods;
        vm.thiscid = cid;
        $(".enlarge_style").fadeIn(300);
        //$(".enlarge_imgstyle").css({top:(WinHeight-ImgHeight)/2+"px"});
    }
    function enlargeClose() {
        $(".enlarge_style").fadeOut(300);
        setTimeout(function () {
            vm.thisgoods = {};
        }, 300)
    }

    function addthis(cid, thisgoods) {
        console.log(thisgoods);
        vm.add(cid, thisgoods);
        vm.enlargeClose();
    }
    vm.shopbuygoods = ShopMenuService.getShopCarByShopid(vm.shopid);
    vm.goodscontent = ShopMenuService.gettotalpriceandcount(vm.shopid);
    vm.limitText = "本店已打烊";
    var float_show = false;
    $(".store_background_hide").click(function () {
        in_out("out");
    });
    function store_hide() {
        if ($cookies.get("shopbuygoods")) {
            vm.shopbuygoods = JSON.parse($cookies.get("shopbuygoods"));
        } else {
            vm.shopbuygoods = ShopMenuService.getShopCarByShopid(vm.shopid);
        }
        if (vm.shopbuygoods.length > 0) {
            if (float_show) {
                in_out("out");
            } else {
                in_out("in");
            }
        }
    }
    function go_back() {
        window.history.go(-1);
    }

    function add(cid, goods, cartGoods) {//增加按钮
        if (cartGoods) {
            ShopMenuService.addGood(vm.shopid, cid, goods);
            vm.goodscontent = ShopMenuService.gettotalpriceandcount(vm.shopid);
            vm.shopbuygoods = ShopMenuService.getShopCarByShopid(vm.shopid);
            setOrderNumbers(vm.goodscontent);
            vm.float_more();
            return;
        } else {
            if (goods.goodsSkuList.length > 1) {
                vm.skuGood = goods;
                vm.skuList = goods.goodsSkuList;
                for (var i = 0; i < vm.skuList.length; i++) {
                    if (i == 0) {
                        vm.skuList[i].choose = true;
                        vm.skuChoose = vm.skuList[i];
                    } else {
                        vm.skuList[i].choose = false;
                    }
                }
                $(".specpanel-2yGNl").show();
                $(".specpanel-2HhFg").show();
                $(".specpanel-3_81U").show();
                setTimeout(function () {
                    $(".specpanel-2HhFg").addClass("specpanel-EyF12");
                    $(".specpanel-3_81U").addClass("specpanel-3Y186");
                }, 10);
            } else {
                ShopMenuService.addGood(vm.shopid, cid, goods);
                vm.goodscontent = ShopMenuService.gettotalpriceandcount(vm.shopid);
                vm.shopbuygoods = ShopMenuService.getShopCarByShopid(vm.shopid);
                setOrderNumbers(vm.goodscontent);
                vm.float_more();
            }
        }
        vm.addSuk = function (sku) {
            ShopMenuService.addGood(vm.shopid, cid, goods, sku);
            vm.goodscontent = ShopMenuService.gettotalpriceandcount(vm.shopid);
            vm.shopbuygoods = ShopMenuService.getShopCarByShopid(vm.shopid);
            setOrderNumbers(vm.goodscontent);
            vm.closeSku();
            vm.float_more();
        }
    }
    function remove(cid, goods, cartGoods) {//删除按钮
        if (!cartGoods && goods.num > 1 && goods.goodsSkuList.length > 1) {
            input_alert("多规格商品请到清单中删除");
            return;
        }
        ShopMenuService.removeGood(vm.shopid, cid, goods);
        vm.shopbuygoods = ShopMenuService.getShopCarByShopid(vm.shopid);
        vm.goodscontent = ShopMenuService.gettotalpriceandcount(vm.shopid);
        setOrderNumbers(vm.goodscontent);
        if (vm.goodscontent.totalprice == 0) {
            in_out("out");
        }
        vm.float_more();
    }
    function notrash() {
        $(".contain-item").fadeOut();
    }
    function sure_clean() {
        $(".contain-item").fadeIn();
        vm.trash = function () {//清空
            ShopMenuService.removeShopById(vm.shopid);
            vm.shopbuygoods = ShopMenuService.getShopCarByShopid(vm.shopid);
            vm.goodscontent = ShopMenuService.gettotalpriceandcount(vm.shopid);
            $(".contain-item").fadeOut();
            in_out("out");
        }
    }

    function float_more() {
        var height = $("#float_more").height();
        var hei = $("#float_more");
        if (vm.shopbuygoods.length > 6) {
            hei.addClass("float_more");
        } else {
            hei.removeClass("float_more");
        }
    }
    //立即购买操作
    //结算按钮
    function gosettle() {
        var shopList = ShopMenuService.buyNowShopById(vm.shopid);
        if (!shopList) {
            input_alert("您清单为空");
        } else {
            loadCart(true, "leaveLoader", ".loader_bac");
            $state.go("menu", {
                shopid: vm.shopid,
                appid: $stateParams.appid,
                deskid: vm.deskid,
                order: shopList
            });
        }
    }
    function setOrderNumbers(goodscontent) {
        if (goodscontent.count > 99) {
            goodscontent.count = "99+";
        }
    }

    //tab切换函数
    var check_tab = ShopService.getTag();
    if (check_tab) {
        if (check_tab == "message") {
            $("#change3").find("a").click();
            $(".store_bottom").fadeOut();
        }
    }

    function tab($event) {
        var self_href = $event.target.getAttribute("aria-controls");
        ShopService.setTag(self_href);
        if (self_href == "message") {
            $(".store_bottom").fadeOut();
        }
        else {
            $(".store_bottom").fadeIn();
        }
    }


    //关闭sku选择
    function closeSku() {
        $(".specpanel-2HhFg").removeClass("specpanel-EyF12");
        $(".specpanel-3_81U").removeClass("specpanel-3Y186");
        setTimeout(function () {
            $(".specpanel-2HhFg").hide();
            $(".specpanel-3_81U").hide();
            $(".specpanel-2yGNl").hide();
        }, 200);
    }
    //选择sku
    function chooseSku(sku) {
        for (var i = 0; i < vm.skuList.length; i++) {
            vm.skuList[i].choose = false;
        }
        sku.choose = true;
        vm.skuChoose = sku;
    }
    //购物车显示隐藏
    function in_out(status) {
        if (status == "in") {
            $(".store_detail").show();
            $(".store_background_hide").show();
            setTimeout(function () {
                $(".store_list").removeClass("store_list_down").addClass("store_list_up");
                $(".store_background_hide").addClass("store_background_active");
            }, 10);
            float_show = true;
        } else if (status == "out") {
            $(".store_list").removeClass("store_list_up").addClass("store_list_down");
            $(".store_background_hide").removeClass("store_background_active");
            setTimeout(function () {
                $(".store_detail").hide();
                $(".store_background_hide").hide();
            }, 300);
            float_show = false;
        }
    }

    /*个人中心标签页*/
    //个人信息显示
    if ($cookies.get("_userId")) {
        AuthService.getuser($cookies.get("_userId")).$promise.then(function (data) {
            //alert(JSON.stringify(data))
            vm.user = data;
        })
    } else if (!$cookies.get("_userId")) {
        vm.user = {
            "name": "请点击登录",
            "nickname": "请点击登录"
        }
    }


    //个人中心登录按钮
    function gologin() {
        if ($cookies.get("_userId")) {
            //$state.go("login");
            $state.go("myview", {shopid: $stateParams.shopid});
        } else if (!$cookies.get("_userId")) {
            $state.go("login");
        }
    }

    //历史订单
    function goorderhistory() {
        $state.go("orderhistory", {shopid: vm.shopid, appid: vm.appid, deskid: vm.deskid});
    }

    //店铺信息
    function goshopinfo() {
        $state.go("shopinfo", {shopid: vm.shopid});
    }
    //店铺优惠券跳转
    function shop_coupon() {
        $state.go("receivecoupon");
    }

    //呼叫服务员
    //弹出电话显示隐藏方法
    function showphone(status) {
        if (status == "in") {
            $(".shop_detail").show();
            $(".shop_background_hide").show();
            setTimeout(function () {
                $(".shop_list").removeClass("shop_list_down").addClass("shop_list_up");
                $(".shop_background_hide").addClass("shop_background_active");
            }, 10);
            phone_show = true;
        } else if (status == "out") {
            $(".shop_list").removeClass("shop_list_up").addClass("shop_list_up");
            $(".shop_background_hide").removeClass("shop_background_active");
            setTimeout(function () {
                $(".shop_detail").hide();
                $(".shop_background_hide").hide();
            }, 300);
            phone_show = false;
        }
    }

    //默认设置
    var phone_show = false;
    //点击电话按钮
    function phone_hide() {
        if (phone_show) {
            showphone("out");
        } else {
            showphone("in");
        }
    }
    //点击空白处隐藏
    $(".shop_background_hide").click(function () {
        showphone("out");
    });
    //呼叫服务员结束
}